axios.defaults.baseURL="http://localhost:8080"
Vue.prototype.$http=axios
//1.axios前置拦截器：每次通过sxios发送请求之前，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})

//2.axios后置拦截器：后端处理完axios请求之后，处理响应的数据
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.msg==="nologin") {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href = "login.html";
    }
    if(!data.success && data.msg==="timeOut") {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href = "login.html";
    }
    return result;
},error => {
    Promise.reject(error);
})
let url = location.href;
if( url.indexOf("register.html")==-1 && url.indexOf("login.html")==-1
    && url.indexOf("callback.html")==-1 && url.indexOf("binder.html")==-1
    )
{
    let logininfoStr=localStorage.getItem("logininfo");
    if(!logininfoStr){
        location.href="login.html";
    }
}
//封装公用方法 - 将字符串的参数解析为json对象
function parseUrlParams2Obj(url) {
    let paramStr = url.substring(url.indexOf("?")+1);//xxx?code=xxx&state=1
    let paramArr = paramStr.split("&");
    let paramObj = {};
    for(let i = 0;i<paramArr.length;i++){
        let paramTemp = paramArr[i];
        let paramName = paramTemp.split("=")[0];
        let paramValue= paramTemp.split("=")[1];
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}
